package com.rising.threads.threadlife;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
 * @Author: rising
 * @Description:
 * @Date: create in 2021/3/7 9:46
 */
public class ThreadMain {

    public static void main(String[] args) {
        testThreadPools();
        //testCallable();
        //testThreadReStart();
    }

    //测试线程池
    public static void testThreadPools() {
        ExecutorService executorService = new ThreadPoolExecutor(
                10,
                20,
                1,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20), new ThreadFactory() {
                    AtomicInteger threadNums = new AtomicInteger(1);
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r, "第" + threadNums.getAndIncrement() + "个线程");
                        return thread;
                    }});
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 开始执行任务");
            }
        };
        for (int i = 0; i < 40; i++) {
            executorService.submit(runnable);
        }

        System.out.println("----------------------------");
        try {
            Thread.sleep(6 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread mainThread = Thread.currentThread();
        ThreadGroup mainThreadThreadGroup = mainThread.getThreadGroup();
        //获取线程组中的线程。
        int count = mainThreadThreadGroup.activeCount();
        System.out.println("count:"+count);
        Thread[] threads = new Thread[count];
        //enumerate 枚举，recurse 递归
        mainThreadThreadGroup.enumerate(threads, true);
        Stream.of(threads).filter(Thread::isAlive).forEach(thread -> System.out.println("方法2：线程池的线程：" + thread ));
    }

    /**
     * 测试有返回结果的线程
     */
    public static void testCallable() {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName() + " 进来了");
                Thread.sleep(2 * 1000);
                return "这是结果";
            }
        });
        Thread threadA = new Thread(futureTask, "线程A");
        threadA.start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    /**
     * 线程消亡后不可以重新start
     */
    public static void testThreadReStart() {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 进来了");
            }
        }, "线程A");

        threadA.start();
        try {
            Thread.sleep(1 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadA.start();
    }

}
